﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.ItemMasterP
{
    public class AfterInserting : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.CBO.SCM.Item.ItemMaster;
            if (holder == null)
                return;
            #endregion

            HttpHelper httpHelper = new HttpHelper();
            JsonConfig config = new JsonConfig();

            httpHelper.URL = config.SelectToken("WMS.URL") + "/openapi/sync/material/save";
            WMSItemDTO json = new WMSItemDTO();
            json.code = holder.Code;
            json.text = holder.Name;
            json.baseUnit = new Baseunit
            {
                code = holder.InventoryUOM.Code,
                name = holder.InventoryUOM.Name
            };
            json.supplUnit = new Supplunit
            {
                code = holder.CostUOM.Code,
                name = holder.CostUOM.Name
            };
            json.ratio = 1;
            json.materialTypeCode = holder.MainItemCategory.Code;
            json.spec = holder.SPECS;
            if (holder.IsPurchaseEnable && holder.IsBuildEnable)
                json.sourceType = "BUY_MAKE";
            else if (holder.IsPurchaseEnable)
                json.sourceType = "BUY";
            else if (holder.IsBuildEnable)
                json.sourceType = "MAKE";

            httpHelper.Content = JsonConvert.SerializeObject(json);
            var result = JObject.Parse(httpHelper.HttpPosts());
            if (result["code"].ToString() != "000000")
            {
                throw new Exception(result["msg"].ToString());
            }


        }


    }
}
